/*移动端返回事件处理*/
function judgeOnBack() {
    return 1; //1回到工作台
}
$(function () {
    if (u.indexOf('iPhone') > -1) {   //苹果手机
        setTimeout(function () {
            WebViewJavascriptBridge.callHandler('getPartCode', {'foo': 'bar'}, function(response) {
               /* layer.open({
                    content: response.token
                    ,btn: '确定'
                });*/
                response=JSON.parse(response);
                appToken=response.token;
                appUserId=response.userId;
                localStorage.appToken=appToken;
                localStorage.appUserId=appUserId;
                testModeRecord(2)
            })
        },50);
    }else {
        jsObj.setRight('','');
        var response=jsObj.getPartCode();
        //alert(response);
        response=JSON.parse(response);
        appToken=response.token;
        appUserId=response.userId;
        localStorage.appToken=appToken;
        localStorage.appUserId=appUserId;
        testModeRecord(2)
    }
    iscrol();
    //切换试模菜单
    $('body').on('click', '.TabNavUl li', function () {
        if(type!=parseInt($(this).attr('data-type'))){
            indexPage=1;
            type=parseInt($(this).attr('data-type'));
            testModeRecord(type);
        }
    })

    //试模详情
    $('body').on('click', '.TabMainUl li', function () {
        var historyObj=JSON.parse(historyObjStr).list;
        var len=parseInt($(this).attr('data-len'));
        localStorage.historyId=historyObj[len].id;
        localStorage.assemblCode=historyObj[len].assemblCode;
        localStorage.productName=historyObj[len].productName;
        localStorage.productCode=historyObj[len].productCode;
        localStorage.productOrderCode=historyObj[len].productOrderCode;
        localStorage.partCount=historyObj[len].partCount;
        localStorage.modeNum=modeNum(historyObj[len].listTestModeDetail);
        if (u.indexOf('iPhone') > -1) {   //苹果手机
            location.href='testInfo.html';
        }else {
            jsObj.openHyperLinkReference('testInfo.html');
        }
    })

})



function modeNum(num) {
    var modeNumStr=[];
    for(var j=0;j<num.length;j++){
        if(num[j].status==2){
            modeNumStr.push('T'+num[j].tCount);
        }else{
            modeNumStr.push('T'+num[j].tCount+'进行中');
        }
    }
    return modeNumStr.join('、');
}

var type=2;

var myScroll,
    pullDownEl, pullDownOffset,
    pullUpEl, pullUpOffset,indexPage=1,pageNum=1;

var historyObjStr='';
function testModeRecord(type) {
    //layer.open({type: 2,shadeClose: false,});
    $('.spinner').show();
    var url='/test/mode/record';
    var mdObj=md5Fun(appUserId,url,appToken);
    var parameterObj= {
        "type":type,
        "indexPage":indexPage,
        "size":10
    };
    var parameter=Encrypt(JSON.stringify(parameterObj));
    console.log(Decrypt(parameter));
    $.ajax({
        type: "POST",
        timeout : 5000,
        url: webUrlCon+url,
        data:JSON.stringify(
            {"parameter":parameter}
        ),
        beforeSend: function(request) {
            request.setRequestHeader("Content-Type", "application/json");
            request.setRequestHeader("sequence", mdObj.sequence);
            request.setRequestHeader("nonceStr", mdObj.nonceStr);
            request.setRequestHeader("userId", appUserId);
            request.setRequestHeader("token",appToken);
            //xmlhttp.setRequestHeader("infName", url);
            //xmlhttp.setRequestHeader("osVersion", '');
            request.setRequestHeader("versionType", "1");
            request.setRequestHeader("osPlatform", mdObj.osPlatform);
            request.setRequestHeader("appVersion", mdObj.appVersion);
            request.setRequestHeader("sign", mdObj.sign);
        },
        success: function (data) {
            //layer.closeAll();
            $('.spinner').hide();
            $('.main').show();
            tabNavRefresh()
            console.log(data);
            if(data.code==100){
                content=Decrypt(data.content);
                //alert(content)
                historyObjStr=content;
                content=JSON.parse(content);
                console.log(content)
                var historyHtml='';
                if(content.list.length>0){
                    for(var i=0;i<content.list.length;i++){
                        historyHtml+='<li data-len='+i+'>' +
                            '<div class="title">试模产品编号：'+content.list[i].assemblCode+'</div>' +
                            '<div class="content">' +
                            '<p><span>产品名称：</span>'+content.list[i].productName+'</p>' +
                            '<p><span>产品编号：</span>'+content.list[i].productCode+'</p>' +
                            '<p><span>生产单号：</span>'+content.list[i].productOrderCode+'</p>' +
                            '<p><span>零件总数：</span>'+content.list[i].partCount+'</p>' +
                            '<p><span>完成试模：</span>'+modeNum(content.list[i].listTestModeDetail)+'</p>' +
                            '</div>' +
                            '</li>';
                    }
                }else{
                    historyHtml='<div style="text-align: center; color: #888888; padding-top: 1rem; font-size: 0.35rem;">暂无数据</div>';
                }
                if(indexPage==1){
                    $('.TabMainUl').html(historyHtml);
                }else{
                    $('.TabMainUl').append(historyHtml);
                }
                pageNum=data.content.pageNum;
                if(data.content.pageNum>10){
                    pullUpEl = document.getElementById('pullUp');
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
                    pullUpEl.className = '';
                    document.getElementById('pullUp').style.display='block';
                }else{
                    document.getElementById('pullUp').style.display='none';
                }
                myScroll.refresh();
            }else{
                layer.open({
                    content: data.code+' '+data.message
                    ,btn: '确定'
                });
            }
        },
        error: function(e) {
            //layer.closeAll();
            $('.spinner').hide();
            layer.open({
                content: '无法请求服务器，请稍后再试！'
                ,btn: '确定'
            });
        }
    });
}



function pullDownAction () {
    console.log('pullDownAction')
    indexPage=1
    testModeRecord(type);
}
function pullUpAction () {
    console.log('pullUpAction')
    indexPage++;
    if(pageNum>=indexPage){
        testModeRecord(type);
    }else{
        pullUpEl = document.getElementById('pullUp');
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '已加载全部';
        pullUpEl.className = '';
    }
}
function iscrol() {
    pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = pullUpEl.offsetHeight;

    myScroll = new iScroll("wrapper",{
        topOffset:50,
        onScrollStart:function(){
            //console.log('onScrollStart');
        },
        onScrollMove:function(){
            /*console.log('onScrollMove');
            console.log('this.y='+this.y);
            console.log('this.maxScrollY='+this.maxScrollY);*/

            //头部刷新
            if (this.y > 10 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新';
                this.minScrollY = 0;
            }
            else if (this.y < 10 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新';
                this.minScrollY = -pullDownOffset;
            }
            else if (this.y < (this.maxScrollY - 10) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放加载更多';
                this.maxScrollY = this.maxScrollY;
            }
            else if (this.y > (this.maxScrollY + 10) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放刷新';
                this.maxScrollY = pullUpOffset;
            }
        },
        onTouchEnd:function(){
            //console.log('onTouchEnd');
        },

        onScrollEnd:function(){
            //console.log('onScrollEnd');

            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
                pullDownAction();
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
                pullUpAction();
            }
        },
        onRefresh:function(){
            //console.log('onRefresh');
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
            }
        }
    });
}

/*
* tabNav 菜单切换
* */
tabNavInit()
function tabNavInit() {
    $('body').on('click', '.TabNavUl li', function () {
        $(this).parent('.TabNavUl').children('li').removeClass('active');
        $(this).addClass('active');
        $(this).siblings('.lanPos').css({
            'left': $(this).get(0).offsetLeft,
            'width': $(this).children('span').get(0).clientWidth
        });
        $(this).parent('.TabNavUl').animate({scrollLeft: $(this).get(0).offsetLeft - ($('.TabNavUl').width() / 2) + ($(this).children('span').get(0).clientWidth / 2)}, 300);
    });
}
function tabNavRefresh() {
    $('.TabNavUl').each(function () {
        if ($(this).find('.lanPos').length == 0) {
            $(this).append('<div class="lanPos"></div>');
        }
        $(this).find('.lanPos').css({
            'left': $(this).find('li.active').get(0).offsetLeft,
            'width': $(this).find('li.active').children('span').get(0).clientWidth
        });
        $(this).animate({scrollLeft: $(this).find('li.active').get(0).offsetLeft - ($('.TabNavUl').width() / 2) + ($(this).find('li.active').children('span').get(0).clientWidth / 2)}, 1);
    })
}